package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
)

//限制重定向次数
func redirectLimitTimes() {
	client := &http.Client{
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			if len(via) > 1 {
				return errors.New("redirect too times")
			}
			return nil
		},
	}
	r, _ := http.NewRequest(http.MethodGet, "http://httpbin.org/absolute-redirect/100000000", nil)
	w, err := client.Do(r)

	if err != nil {
		fmt.Println("err = ", err)
	}
	fmt.Println(ioutil.ReadAll(w.Body))
}

//重定向
func main() {
	// redirectLimitTimes()
	redirectForbidden()

}

func redirectForbidden() {
	//禁止重定向
	//登录请求,防止重定向到首页
	client := &http.Client{
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		},
	}

	r, _ := http.NewRequest(http.MethodGet, "http://httpbin.org/cookies/set?freeform=ding", nil)

	r2, err := client.Do(r)
	if err != nil {
		panic(err)
	}
	fmt.Println(r2.Request.URL)
}
